#!/usr/bin/env python

##  flamingo - 2D Game Engine
##  Copyright (C) 2009 Bradley Zeis
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##  Bradley Zeis
##  bradzeis@gmail.com

import sys

from . import event

_BACKEND = None
_INITIALIZED = False

def load(display_name, event_name):
    """Load a display backend.
    
    If the display backend has already been initialized, there will be no effect.
    
    Return: None
    """
    if _INITIALIZED:
        return
    
    try:
        global _BACKEND
        __import__(display_name)
        _BACKEND =  sys.modules[display_name]
        
        event.load(event_name)
        event.init()
        
    except ImportError:
        raise ImportError, "could not import backend '{0}'".format(display_name)

def init(width, height, caption, fullscreen=0, depth=0, flags=0):
    """Initialize the display.
    
    If a backend is not loaded before this is called, an AttributeError is raised.
    
    Backend:
        Backends should not change the values of display._BACKEND or display._INITIALIZED
    
    Return: None
    """
    try:
        _BACKEND.init(width, height, caption, fullscreen, flags, depth)
        
    except AttributeError:
        raise AttributeError, "a display backend has not been loaded"
    
    Display.width = width
    Display.height = height
    Display.caption = caption
    Display.fullscreen = fullscreen
    Display.depth = 0
    Display.flags = 0

def get_init():
    """Return true if the display is initialized.
    
    Return: boolean
    """
    return _INITIALIZED

def quit():
    """Quit the display.
    
    If a backend is not loaded before this is called, an AttributeError is raised.
    
    Return: None
    """
    try:
        _BACKEND.quit()
        
    except AttributeError:
        raise AttributeError, "a display backend has not been loaded"
    
    Display.clear()

def flip():
    """Update the contents on the display surface.
    
    If a backend is not loaded, an AttributeError will be raised.
    
    Return: None
    """
    try:
        _BACKEND.flip()
        
    except AttributeError:
        raise AttributeError, "a display backend has not been loaded"
    
def get_size():
    """Get the caption of the display window.
    
    Return: string
    """
    return Display.width, Display.height

def set_size(width, height):
    """Set the size of the display window.
    
    If the display size can't be changed by the current backend, the backend should
    do nothing instead of raising an exception.
    
    Return: None
    """
    try:
        _BACKEND.set_caption(width, height)
        
    except AttributeError:
        raise AttributeError, "a display backend has not been loaded"

def get_caption():
    """Get the caption of the display window.
    
    Return: string
    """
    return Display.caption

def set_caption(caption):
    """Set the caption of the display window.
    
    Return: None
    """
    try:
        _BACKEND.set_caption(caption)
        
    except AttributeError:
        raise AttributeError, "a display backend has not been loaded"

class Display(object):
    """Holds information about the display surface."""
    width = 0
    height = 0
    caption = ""
    fullscreen = 0
    depth = 0
    flags = 0
    
    @classmethod
    def clear(cls):
        width = 0
        height = 0
        caption = 0
        fullscreen = 0
        depth = 0
        flags = 0
